<?php
class ModelMessageActivity extends Model {
	public function getTotalActivitys($data) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "message_activity`";
		$sql .= $this->filterSql($sql);
      	$query = $this->db->query($sql);
      	
		return $query->row['total'];
	}
	
	private function filterSql($data){
		$sql ="";
		if(isset($data['filter_customer_id'])&&!empty($data['filter_customer_id'])){
			$sql .= " and mta.customer_id ='".(int)$data['filter_customer_id']."'";
		}
		
		if(isset($data['filter_date_end'])&&!empty($data['filter_date_end'])){
			$sql .= " and mta.end_date >='".$data['filter_date_end']."'";
		}
		
		return $sql;
	}
	
	
	
	public function getActivitys($data = array()) {
		$sql = "SELECT ma.* FROM  ".DB_PREFIX."customer_to_activity mta left join `" . DB_PREFIX . "message_activity` ma on(ma.activity_id=mta.activity_id)   where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'ma.date_start'
		);	
		
		$sql .=" ORDER BY ma.date_start desc";
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	
	public function getAllActivitys($data = array()) {
		$sql = "SELECT ma.* FROM  `" . DB_PREFIX . "message_activity` ma  where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'ma.date_start'
		);	
		$sql .=" ORDER BY ma.date_start desc";
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	
	public function getActivity($data=array()){
		$sql = "SELECT * FROM `" . DB_PREFIX . "message_activity`  where activity_id='".(int)$data['activity_id']."'";
		return $this->db->query($sql)->row;
	}
	
	
	public function enter($activity_id,$customer_id){
		$sql = "insert into  `" . DB_PREFIX . "customer_to_activity`  set activity_id='".(int)$activity_id."',customer_id='".(int)$customer_id."'";
		$this->db->query($sql);
		
		$this->db->query("update ".DB_PREFIX."message_activity set used=used+1 where activity_id='".(int)$activity_id."'");
	}
	
	
	public function check($activity_id,$customer_id){
		
		$sql = "select * from ".DB_PREFIX."message_activity where activity_id='".(int)$activity_id."'" ;
		$result = $this->db->query($sql)->row;
		if($result){
			if((int)$result['limit']>(int)$result['used']){
				$sql = "select count(*) as total from ".DB_PREFIX."customer_to_activity where customer_id='".(int)$customer_id."' and activity_id='".(int)$activity_id."'";
				$total = (int)$this->db->query($sql)->row['total'];
				if($total==0){
					return true;
				}
			}	
		}
		return false;
	}
	
	
}
?>